package com.jxc.jxc01.hch.web;

import com.alibaba.fastjson2.JSONObject;
import com.jxc.jxc01.hch.entity.Commodity;
import com.jxc.jxc01.hch.entity.PageHelper;
import com.jxc.jxc01.hch.entity.RespResult;
import com.jxc.jxc01.hch.entity.User;
import com.jxc.jxc01.hch.service.IListService;
import com.jxc.jxc01.hch.service.Impl.IListServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("*.Commodity")
public class CommodityServlet extends HttpServlet {
    IListService listService = new IListServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String uri = req.getRequestURI();
        System.out.println("==>"+uri);
        switch (uri){
            case "/jxc01/listCommodity.Commodity":
                listCommodity(req,resp);
                break;
            case "/jxc01/update.Commodity":
                update(req,resp);
                break;
            case "/jxc01/delete.Commodity":
                delete(req,resp);
                break;
            case "/jxc01/insert.Commodity":
                System.out.println("insert.Commodity");
                insert(req,resp);
                break;
            case "/jxc01/query.Commodity":
                query(req,resp);
                break;

        }
    }

    /**
     * 商品资料添加
     * @param req
     * @param resp
     */
    private void insert(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Commodity commodity = new Commodity();
            commodity.setName(req.getParameter("name"));
            commodity.setClassify(Integer.parseInt(req.getParameter("classify")));
            commodity.setSpecs(req.getParameter("specs"));
            commodity.setPrice(Double.parseDouble(req.getParameter("price")));
            listService.insert(commodity);
            resp.sendRedirect("shangpinziliao.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 商品资料模糊查询
     * @param req
     * @param resp
     */
    private void query(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PageHelper<Commodity> pageHelper = new PageHelper<>();
            PrintWriter out = resp.getWriter();
            //获取页码页容
//            int pageNumber = Integer.parseInt(req.getParameter("pagenumber"));
//            int pageSize = Integer.parseInt(req.getParameter("pagesize"));
            int pageNumber = 1;
            int pageSize = 10;
            String name = req.getParameter("name");
            pageHelper.setPageNum(pageNumber);
            pageHelper.setPageSize(pageSize);
            pageHelper = listService.query(name,pageSize,pageNumber);
            HttpSession session = req.getSession();
            session.setAttribute("query",pageHelper);
            RespResult respResult = new RespResult(0,"ok",pageHelper.getTotal(),pageHelper.getData());
            out.print(JSONObject.toJSONString(respResult));
            resp.sendRedirect("shangpinziliao.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 商品资料删除
     * @param req
     * @param resp
     */
    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        try {
            int id = Integer.parseInt(req.getParameter("id"));
            listService.delete(id);
            resp.sendRedirect("shangpinziliao.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 商品资料修改
     * @param req
     * @param resp
     */
    private void update(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Commodity commodity = new Commodity();
            //获取请求数据
            commodity.setId(Integer.parseInt(req.getParameter("id")));
            commodity.setName(req.getParameter("name"));
            commodity.setClassify(Integer.parseInt(req.getParameter("classify")));
            commodity.setSpecs(req.getParameter("specs"));
            commodity.setPrice(Double.parseDouble(req.getParameter("price")));
            listService.update(commodity);
            resp.sendRedirect("shangpinziliao.html");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 商品资料查询
     * @param req
     * @param resp
     */
    private void listCommodity(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PageHelper<Commodity> pageHelper = new PageHelper<>();
            PrintWriter out = resp.getWriter();
            //获取页码页容
            int pageNumber = Integer.parseInt(req.getParameter("pagenumber"));
            int pageSize = Integer.parseInt(req.getParameter("pagesize"));
            pageHelper.setPageNum(pageNumber);
            pageHelper.setPageSize(pageSize);
            HttpSession session = req.getSession();
            System.out.println(session);
            if (session.getAttribute("query")==null) {
                pageHelper = listService.listCommodity(pageSize, pageNumber);
            }else {
                pageHelper= (PageHelper<Commodity>) session.getAttribute("query");
            }
            RespResult respResult = new RespResult(0,"ok",pageHelper.getTotal(),pageHelper.getData());
            out.print(JSONObject.toJSONString(respResult));
            session.removeAttribute("query");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
